/*
 * GuildFraktionInfo - list the reputation and the guild level of all members of a wow guild
 * Copyright (C) [2012 - 2013] [www.marantis.de]
 * 
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public 
 * License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later 
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied 
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this program; if not, 
 * see <http://www.gnu.org/licenses/>.
 */
package de.marantis.app.wow.gfi.helper;

import de.marantis.app.wow.gfi.beans.ItemLevelBean;
import org.apache.log4j.Logger;

/**
 * In this class we analyze the [armoryContent] character (ex. for the iLvL)<br>
 * <br> 
 * Expl: http://eu.battle.net/wow/de/character/[REALM]/[CHAR]/simple
 */
public class ArmoryCharacterHelper {

    private final static Logger log = Logger.getLogger(ArmoryCharacterHelper.class);
    
    public static ItemLevelBean getItemLevel(String armoryContent) {
        try {
            if (armoryContent == null || armoryContent.length() <= 0) {
                return null;
            }

            /*
             * *** EQUIPPED ITEM LEVEL *******************************************************************************
             */
            
            //*** jump to the iLvL DIV ***
            String search = "<div class=\"summary-averageilvl\">";
            int pos = armoryContent.indexOf(search);
            if (pos < 0) {
                return null;
            }

            //*** jump to the equipped iLvL number ***
            search = "<span class=\"equipped\">";
            pos = armoryContent.indexOf(search, pos);
            if (pos < 0) {
                return null;
            }
            
            pos += search.length();
            
            //*** jump to the next '<' ***
            search = "<";
            int pos2 = armoryContent.indexOf(search, pos);
            if (pos2 < 0) {
                return null;
            }
            
            //*** equipped iLvL number ***
            String strEquippedItemLvl = armoryContent.substring(pos, pos2);
            strEquippedItemLvl = strEquippedItemLvl.trim();
            int equippedItemLvl = CommonHelper.String2Int(strEquippedItemLvl);
            if (equippedItemLvl < 0) {
                return null;
            }

            /*
             * *** AVERAGE ITEM LEVEL *******************************************************************************
             */
            
            //*** jump to the average iLvL number ***
            search = "data-id=\"averageilvl\">";
            pos = armoryContent.indexOf(search, pos2);
            if (pos < 0) {
                return null;
            }
            
            pos += search.length();
            
            //*** jump to the next '<' ***
            search = "<";
            pos2 = armoryContent.indexOf(search, pos);
            if (pos2 < 0) {
                return null;
            }
            
            //*** average iLvL number ***
            String strAverageItemLvl = armoryContent.substring(pos, pos2);
            strAverageItemLvl = strAverageItemLvl.trim();
            int averageItemLvl = CommonHelper.String2Int(strAverageItemLvl);
            if (averageItemLvl < 0) {
                return null;
            }
            
            //*** return reputation result ***
            ItemLevelBean itemLevelBean = new ItemLevelBean();
            itemLevelBean.setEquippedItemLvl(equippedItemLvl);
            itemLevelBean.setAverageItemLvl(averageItemLvl);
            return itemLevelBean;

        } catch (Exception e) {
            log.error(e);
            return null;
        }
    }

}
